package com.sun.service.ums.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.mapper.ums.UmsMenuMapper;
import com.sun.pojo.UmsMenu;
import com.sun.pojo.UmsRole;
import com.sun.mapper.ums.UmsRoleMapper;
import com.sun.service.ums.IUmsRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author 妙妙屋
 * @since 2021-12-13
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements IUmsRoleService {
    @Autowired
    private UmsRoleMapper umsRoleMapper;
    @Autowired
    private UmsMenuMapper umsMenuMapper;
    @Override
    public IPage<UmsRole> selectPage(String keyword, Long pageNum, Long pageSize) {
        //第一个参数是分页参数
        Page<UmsRole> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        //查询条件
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(keyword)) {
            wrapper.like("name", keyword);
        }

        Page<UmsRole> rolePage = umsRoleMapper.selectPage(page, wrapper);
        return rolePage;
    }

    @Override
    public List<UmsMenu> listMenu(Long id) {

        return umsMenuMapper.listMenuByRoleId(id);
    }

    @Override
    public void deleteAllMenu(Long roleId) {
        umsRoleMapper.deleteAllMenu(roleId);
    }

    @Override
    public void addMenu(Long roleId, Long menuId) {
        Map map = new HashMap<>();
        map.put("roleId",roleId);
        map.put("menuId",menuId);
        umsRoleMapper.addMenu(map);
    }
}
